<?php

namespace app\service;

use app\controller\SystemUser;
use app\model\OrderDeal;
use app\model\OrderEnterpriseExchange;

class OrderCertReverseCase
{
    public function spliceWhere($params = [])
    {
        $model = new OrderEnterpriseExchange();
        $c_ids = $model->getIds();
        if (isset($params['certificate_code']) && $params['certificate_code']) {
//            $list = $model
//                ->where('certificate_code', 'like', '%' . $params['certificate_code'] . '%')
//                ->field('id')
//                ->select()
//                ->toArray();
//            $ids = array_column($list, 'id');
            $ids = $model
                ->where('certificate_code', 'like', '%' . $params['certificate_code'] . '%')
                ->column('id');
            //两个数组的交集并赋值
            $c_ids = $this->handlePersonIds($c_ids, $ids);
        }
        //创建时间
        if ((isset($params['create_start_date']) && $params['create_start_date']) && (isset($params['create_end_date']) && $params['create_end_date'])) {
            $start_time = $params['create_start_date'] . ' 00:00:00';
            $end_time = $params['create_end_date'] . ' 23:59:59';
//            $list = $model->where('create_date', 'between', [$start_time, $end_time])
//                ->field('id')
//                ->select()
//                ->toArray();
//            $ids = array_column($list, 'id');
            $ids = $model->where('create_date', 'between', [$start_time, $end_time])
                ->column('id');
            //两个数组的交集并赋值给person_ids
            $c_ids = $this->handlePersonIds($c_ids, $ids);
        }
        //销售时间
//        if ((isset($params['start_sale_date']) && $params['start_sale_date']) && (isset($params['end_sale_date']) && $params['end_sale_date'])) {
//            $start_time = $params['start_sale_date'] . ' 00:00:00';
//            $end_time = $params['end_sale_date'] . ' 23:59:59';
////            $list = $model->where('sale_date', 'between', [$start_time, $end_time])
////                ->field('id')
////                ->select()
////                ->toArray();
////            $ids = array_column($list, 'id');
//            $ids = $model->where('sale_date', 'between', [$start_time, $end_time])
//                ->column('id');
//            //两个数组的交集并赋值给person_ids
//            $c_ids = $this->handlePersonIds($c_ids, $ids);
//        }
        //订单状态
//        if (isset($params['status']) && $params['status']) {
//            $d_model = new OrderDeal();
////            $list = $d_model
////                ->where('status', $params['status'])
////                ->select()
////                ->toArray();
////            $code_ids = array_column($list, 'certificate_code');
////            $list = $model
////                ->where('certificate_code', 'in', $code_ids)
////                ->field('id')
////                ->select()
////                ->toArray();
////            $ids = array_column($list, 'id');
//
//            $code_ids = $d_model
//                ->where('status', $params['status'])
//                ->column ('certificate_code');
//
//            $ids = $model
//                ->where('certificate_code', 'in', $code_ids)
//                ->column('id');
//            //两个数组的交集并赋值
//            $c_ids = $this->handlePersonIds($c_ids, $ids);
//        }
        //聘用单位
        if (isset($params['employ_unit_title']) && $params['employ_unit_title']) {
//            $list = $model
//                ->where('employ_unit_title', 'like', '%' . $params['employ_unit_title'] . '%')
//                ->field('id')
//                ->select()
//                ->toArray();
//            $ids = array_column($list, 'id');
            $ids = $model
                ->where('employ_unit_title', 'like', '%' . $params['employ_unit_title'] . '%')
                ->column('id');
            //两个数组的交集并赋值
            $c_ids = $this->handlePersonIds($c_ids, $ids);
        }
        //聘用单位联系人
        if (isset($params['employ_unit_name']) && $params['employ_unit_name']) {
//            $list = $model
//                ->where('employ_unit_name', 'like', '%' . $params['employ_unit_name'] . '%')
//                ->field('id')
//                ->select()
//                ->toArray();
//            $ids = array_column($list, 'id');
            $ids = $model
                ->where('employ_unit_name', 'like', '%' . $params['employ_unit_name'] . '%')
                ->column('id');
            //两个数组的交集并赋值
            $c_ids = $this->handlePersonIds($c_ids, $ids);
        }
        //聘用单位联系人方式
        if (isset($params['employ_unit_mobile']) && $params['employ_unit_mobile']) {
//            $list = $model
//                ->where('employ_unit_mobile', $params['employ_unit_mobile'])
//                ->field('id')
//                ->select()
//                ->toArray();
//            $ids = array_column($list, 'id');
            $ids = $model
                ->where('employ_unit_mobile', $params['employ_unit_mobile'])
                ->column('id');
            //两个数组的交集并赋值
            $c_ids = $this->handlePersonIds($c_ids, $ids);
        }
        //注册单位
        if (isset($params['unit_reg_title']) && $params['unit_reg_title']) {
//            $list = $model
//                ->where('unit_reg_title', 'like', '%' . $params['unit_reg_title'] . '%')
//                ->field('id')
//                ->select()
//                ->toArray();
//            $ids = array_column($list, 'id');
            $ids = $model
                ->where('unit_reg_title', 'like', '%' . $params['unit_reg_title'] . '%')
                ->column('id');
            //两个数组的交集并赋值
            $c_ids = $this->handlePersonIds($c_ids, $ids);
        }

        //出证部门
        if (isset($params['out_org']) && $params['out_org']) {
            $department_ids = SystemUser::getDepartmentAllUser($params['out_org']);
//            $list = $model
//                ->where('out_org', 'in', $department_ids)
//                ->field('id')
//                ->select()
//                ->toArray();
//            $ids = array_column($list, 'id');
            $ids = $model
                ->where('out_org', 'in', $department_ids)
                ->column('id');
            //两个数组的交集并赋值
            $c_ids = $this->handlePersonIds($c_ids, $ids);
        }
        //出证人员
        if (isset($params['out_uid']) && $params['out_uid']) {
//            $list = \app\model\User::where('nickname', 'like', '%' . $params['out_uid'] . '%')
//                ->field('id')
//                ->select()
//                ->toArray();
//            $user_ids = array_column($list, 'id');
//            $list = $model
//                ->where('out_uid', 'in', $user_ids)
//                ->field('id')
//                ->select()
//                ->toArray();
//            $ids = array_column($list, 'id');
            $user_ids = \app\model\User::where('nickname', 'like', '%' . $params['out_uid'] . '%')
                ->column('id');
            $ids = $model
                ->where('out_uid', 'in', $user_ids)
                ->column('id');
            //两个数组的交集并赋值
            $c_ids = $this->handlePersonIds($c_ids, $ids);
        }
        //出证价格
        if (
            (
                (isset($params['out_start_price']) && $params['out_start_price'])
                ||
                $params['out_start_price'] ===0
                ||
                $params['out_start_price'] === '0'
            )
            &&
            (
                (isset($params['out_end_price']) && $params['out_end_price'])
                ||
                $params['out_end_price'] ===0
                ||
                $params['out_end_price'] === '0'
            )
        ) {
//            $list = $model->where('out_price', 'between', [$params['out_start_price'], $params['out_end_price']])
//                ->field('id')
//                ->select()
//                ->toArray();
//            $ids = array_column($list, 'id');
            $ids = $model->where('out_price', 'between', [$params['out_start_price'], $params['out_end_price']])
                ->column('id');
            //两个数组的交集并赋值给person_ids
            $c_ids = $this->handlePersonIds($c_ids, $ids);
        }
        //出证单位
        if (isset($params['out_unit']) && $params['out_unit']) {
//            $list = $model
//                ->where('out_unit', 'like', '%' . $params['out_unit'] . '%')
//                ->field('id')
//                ->select()
//                ->toArray();
//            $ids = array_column($list, 'id');
            $ids = $model
                ->where('out_unit', 'like', '%' . $params['out_unit'] . '%')
                ->column('id');
            //两个数组的交集并赋值
            $c_ids = $this->handlePersonIds($c_ids, $ids);
        }
        //出证单位联系人
        if (isset($params['out_name']) && $params['out_name']) {
//            $list = $model
//                ->where('out_name', 'like', '%' . $params['out_name'] . '%')
//                ->field('id')
//                ->select()
//                ->toArray();
//            $ids = array_column($list, 'id');
            $ids = $model
                ->where('out_name', 'like', '%' . $params['out_name'] . '%')
                ->column('id');
            //两个数组的交集并赋值
            $c_ids = $this->handlePersonIds($c_ids, $ids);
        }
        //出证单位联系人方式
        if (isset($params['out_mobile']) && $params['out_mobile']) {
//            $list = $model
//                ->where('out_mobile', $params['out_mobile'])
//                ->field('id')
//                ->select()
//                ->toArray();
//            $ids = array_column($list, 'id');
            $ids = $model
                ->where('out_mobile', $params['out_mobile'])
                ->column('id');
            //两个数组的交集并赋值
            $c_ids = $this->handlePersonIds($c_ids, $ids);
        }
        //证书类别
        if (isset($params['certificate_leibie']) && $params['certificate_leibie']) {
//            $list = $model
//                ->where('certificate_leibie', $params['certificate_leibie'])
//                ->field('id')
//                ->select()
//                ->toArray();
//            $ids = array_column($list, 'id');
            $ids = $model
                ->where('certificate_leibie', $params['certificate_leibie'])
                ->column('id');
            //两个数组的交集并赋值给person_ids
            $c_ids = $this->handlePersonIds($c_ids, $ids);
        }
        //证书类型
        if (isset($params['certificate_leixing']) && $params['certificate_leixing']) {
//            $list = $model
//                ->where('certificate_leixing', $params['certificate_leixing'])
//                ->field('id')
//                ->select()
//                ->toArray();
//            $ids = array_column($list, 'id');
            $ids = $model
                ->where('certificate_leixing', $params['certificate_leixing'])
                ->column('id');
            //两个数组的交集并赋值给person_ids
            $c_ids = $this->handlePersonIds($c_ids, $ids);
        }
        //证书等级
        if (isset($params['certificate_dengji']) && $params['certificate_dengji']) {
//            $list = $model
//                ->where('certificate_dengji', $params['certificate_dengji'])
//                ->field('id')
//                ->select()
//                ->toArray();
//            $ids = array_column($list, 'id');
            $ids = $model
                ->where('certificate_dengji', $params['certificate_dengji'])
                ->column('id');
            //两个数组的交集并赋值给person_ids
            $c_ids = $this->handlePersonIds($c_ids, $ids);
        }
        //证书专业
        if (isset($params['certificate_zhuanye']) && $params['certificate_zhuanye']) {
//            $list = $model
//                ->where('certificate_zhuanye', $params['certificate_zhuanye'])
//                ->field('id')
//                ->select()
//                ->toArray();
//            $ids = array_column($list, 'id');
            $ids = $model
                ->where('certificate_zhuanye', $params['certificate_zhuanye'])
                ->column('id');
            //两个数组的交集并赋值给person_ids
            $c_ids = $this->handlePersonIds($c_ids, $ids);
        }
        //b证
        if (isset($params['certificate_b']) && $params['certificate_b']) {
//            $list = $model
//                ->where('certificate_b', $params['certificate_b'])
//                ->field('id')
//                ->select()
//                ->toArray();
//            $ids = array_column($list, 'id');
            $ids = $model
                ->where('certificate_b', $params['certificate_b'])
                ->column('id');
            //两个数组的交集并赋值给person_ids
            $c_ids = $this->handlePersonIds($c_ids, $ids);
        }
        //社保
        if (isset($params['social_security']) && $params['social_security']) {
//            $list = $model
//                ->where('social_security', $params['social_security'])
//                ->field('id')
//                ->select()
//                ->toArray();
//            $ids = array_column($list, 'id');
            $ids = $model
                ->where('social_security', $params['social_security'])
                ->column('id');
            //两个数组的交集并赋值给person_ids
            $c_ids = $this->handlePersonIds($c_ids, $ids);
        }
        //项目
        if (isset($params['project']) && $params['project']) {
//            $list = $model
//                ->where('project', $params['project'])
//                ->field('id')
//                ->select()
//                ->toArray();
//            $ids = array_column($list, 'id');
            $ids = $model
                ->where('project', $params['project'])
                ->column('id');
            //两个数组的交集并赋值给person_ids
            $c_ids = $this->handlePersonIds($c_ids, $ids);
        }
        //周期
        if (isset($params['cycle']) && $params['cycle']) {
//            $list = $model
//                ->where('cycle', $params['cycle'])
//                ->field('id')
//                ->select()
//                ->toArray();
//            $ids = array_column($list, 'id');
            $ids = $model
                ->where('cycle', $params['cycle'])
                ->column('id');
            //两个数组的交集并赋值给person_ids
            $c_ids = $this->handlePersonIds($c_ids, $ids);
        }
        //是否中转
        if (isset($params['transfer']) && $params['transfer']) {
//            $list = $model
//                ->where('transfer', $params['transfer'])
//                ->field('id')
//                ->select()
//                ->toArray();
//            $ids = array_column($list, 'id');
            $ids = $model
                ->where('transfer', $params['transfer'])
                ->column('id');
            //两个数组的交集并赋值给person_ids
            $c_ids = $this->handlePersonIds($c_ids, $ids);
        }
        //收证部门
        if (isset($params['get_org']) && $params['get_org']) {
            $department_ids = SystemUser::getDepartmentAllUser($params['get_org']);
//            $list = $model
//                ->where('get_org', 'in',$department_ids)
//                ->field('id')
//                ->select()
//                ->toArray();
//            $ids = array_column($list, 'id');
            $ids = $model
                ->where('get_org', 'in',$department_ids)
                ->column('id');
            //两个数组的交集并赋值
            $c_ids = $this->handlePersonIds($c_ids, $ids);
        }
        //收证人员
        if (isset($params['get_uid']) && $params['get_uid']) {
//            $list = \app\model\User::where('nickname', 'like', '%' . $params['get_uid'] . '%')
//                ->field('id')
//                ->select()
//                ->toArray();
//            $user_ids = array_column($list, 'id');
//            $list = $model
//                ->where('get_uid', 'in', $user_ids)
//                ->field('id')
//                ->select()
//                ->toArray();
//            $ids = array_column($list, 'id');

            $user_ids = \app\model\User::where('nickname', 'like', '%' . $params['get_uid'] . '%')
                ->column('id');
            $ids = $model
                ->where('get_uid', 'in', $user_ids)
                ->column('id');
            //两个数组的交集并赋值
            $c_ids = $this->handlePersonIds($c_ids, $ids);
        }
        //收证价格
        if (
            (
                (isset($params['get_start_price']) && $params['get_start_price'])
                ||
                $params['get_start_price'] ===0
                ||
                $params['get_start_price'] === '0'
            )
            &&
            (
                (isset($params['get_end_price']) && $params['get_end_price'])
                ||
                $params['get_end_price'] ===0
                ||
                $params['get_end_price'] === '0'
            )
        ) {
//            $list = $model->where('get_price', 'between', [$params['get_start_price'], $params['get_end_price']])
//                ->field('id')
//                ->select()
//                ->toArray();
//            $ids = array_column($list, 'id');
            $ids = $model->where('get_price', 'between', [$params['get_start_price'], $params['get_end_price']])
                ->column('id');
            //两个数组的交集并赋值给person_ids
            $c_ids = $this->handlePersonIds($c_ids, $ids);
        }
        //人才姓名
        if (isset($params['person_name']) && $params['person_name']) {
//            $list = $model
//                ->where('person_name', 'like', '%' . $params['person_name'] . '%')
//                ->field('id')
//                ->select()
//                ->toArray();
//            $ids = array_column($list, 'id');
            $ids = $model
                ->where('person_name', 'like', '%' . $params['person_name'] . '%')
                ->column('id');
            //两个数组的交集并赋值
            $c_ids = $this->handlePersonIds($c_ids, $ids);
        }
        //身份证号
        if (isset($params['id_card']) && $params['id_card']) {
//            $list = $model
//                ->where('id_card', $params['id_card'])
//                ->field('id')
//                ->select()
//                ->toArray();
//            $ids = array_column($list, 'id');
            $ids = $model
                ->where('id_card', $params['id_card'])
                ->column('id');
            //两个数组的交集并赋值
            $c_ids = $this->handlePersonIds($c_ids, $ids);
        }
        //证书有效期
        if ((isset($params['electronic_certificate_start_date']) && $params['electronic_certificate_start_date']) && (isset($params['electronic_certificate_end_date']) && $params['electronic_certificate_end_date'])) {
            $z_model = new OrderDeal();
            $start_time = $params['electronic_certificate_start_date'] . ' 00:00:00';
            $end_time = $params['create_end_date'] . ' 23:59:59';
//            $list = $z_model->where('electronic_certificate_date', 'between', [$start_time, $end_time])
//                ->field('id')
//                ->select()
//                ->toArray();
//            $code_ids = array_column($list, 'certificate_code');
//            $list = $model
//                ->where('certificate_code', 'in', $code_ids)
//                ->field('id')
//                ->select()
//                ->toArray();
//            $ids = array_column($list, 'id');

            $code_ids = $z_model->where('electronic_certificate_date', 'between', [$start_time, $end_time])
                ->column('certificate_code');
            $ids = $model
                ->where('certificate_code', 'in', $code_ids)
                ->column('id');
            //两个数组的交集并赋值
            $c_ids = $this->handlePersonIds($c_ids, $ids);
        }
        //b证有效期
        if ((isset($params['b_electronic_certificate_start_date']) && $params['b_electronic_certificate_start_date']) && (isset($params['b_electronic_certificate_end_date']) && $params['b_electronic_certificate_end_date'])) {
            $z_model = new OrderDeal();
            $start_time = $params['b_electronic_certificate_start_date'] . ' 00:00:00';
            $end_time = $params['b_electronic_certificate_end_date'] . ' 23:59:59';
//            $list = $z_model->where('b_electronic_certificate_date', 'between', [$start_time, $end_time])
//                ->field('id')
//                ->select()
//                ->toArray();
//            $code_ids = array_column($list, 'certificate_code');
//            $list = $model
//                ->where('certificate_code', 'in', $code_ids)
//                ->field('id')
//                ->select()
//                ->toArray();
//            $ids = array_column($list, 'id');

            $code_ids = $z_model->where('b_electronic_certificate_date', 'between', [$start_time, $end_time])
                ->column('certificate_code');
            $ids = $model
                ->where('certificate_code', 'in', $code_ids)
                ->column('id');
            //两个数组的交集并赋值
            $c_ids = $this->handlePersonIds($c_ids, $ids);
        }
        //注册到期时间
        if ((isset($params['b_reg_electronic_certificate_start_date']) && $params['b_reg_electronic_certificate_start_date']) && (isset($params['b_reg_electronic_certificate_end_date']) && $params['b_reg_electronic_certificate_end_date'])) {
            $z_model = new OrderDeal();
            $start_time = $params['b_reg_electronic_certificate_start_date'] . ' 00:00:00';
            $end_time = $params['b_reg_electronic_certificate_end_date'] . ' 23:59:59';
//            $list = $z_model->where('b_electronic_certificate_date', 'between', [$start_time, $end_time])
//                ->field('id')
//                ->select()
//                ->toArray();
//            $code_ids = array_column($list, 'certificate_code');
//            $list = $model
//                ->where('certificate_code', 'in', $code_ids)
//                ->field('id')
//                ->select()
//                ->toArray();
//            $ids = array_column($list, 'id');
            $code_ids = $z_model->where('b_electronic_certificate_date', 'between', [$start_time, $end_time])
                ->column('certificate_code');
            $ids = $model
                ->where('certificate_code', 'in', $code_ids)
                ->column('id');
            //两个数组的交集并赋值
            $c_ids = $this->handlePersonIds($c_ids, $ids);
        }
        //状态
//        if (isset($params['c_status']) && $params['c_status']) {
//            $list = $model
//                ->where('status', $params['c_status'])
//                ->field('id')
//                ->select()
//                ->toArray();
//            $ids = array_column($list, 'id');
//            //两个数组的交集并赋值
//            $c_ids = $this->handlePersonIds($c_ids, $ids);
//        }
        return $c_ids;
    }

    public function handlePersonIds($enter_ids, $other_ids)
    {
        if (!$enter_ids || !$other_ids) {
            return [];
        }
        if ($enter_ids && $other_ids) {
            $enter_ids = array_intersect($enter_ids, $other_ids);
        } elseif ($enter_ids && !$other_ids) {
            $enter_ids = [];
        } elseif (!$enter_ids && $other_ids) {
            $enter_ids = array_intersect($enter_ids, $other_ids);
        } elseif (!$enter_ids && !$other_ids) {
            $enter_ids = [];
        }
        return $enter_ids;
    }
}